-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for infix wildcard #46
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #46 +/- ##
==========================================
+ Coverage 90.14% 90.46% +0.32%
==========================================
Files 18 18
Lines 2202 2832 +630
==========================================
+ Hits 1985 2562 +577
- Misses 158 210 +52
- Partials 59 60 +1 ☔ View full report in Codecov by Sentry. |
rewiko
approved these changes
Nov 5, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Introduction
This pull request introduces support for infix catch-all wildcard parameters, enhancing the routing flexibility. Developers can now define routes with catch-all parameters (
*{param}
) not only at the end but also in the middle of the route path. This allows for more dynamic and versatile URL matching patterns, catering to a wider range of application requirements.Background and Motivation
Previously, the router supported:
{param}
matching exactly one non-empty path segment.*{param}
matching zero or more segments, but only at the end of a route path.This limitation restricted the ability to define routes that require matching variable-length path segments within the middle of a URL. Infix catch-all parameters allow for more complex and descriptive routing patterns, such as
/*/track
(a feature needed for Wiztrack that was not supported bygin
).Breaking Change
One subtle breaking change has been introduced: catch-all wildcards no longer match empty segments, including at the end of a route. All matches now have to be explicit. However, it's now possible to define routes such as
/path/
and/path/*{any}
to reproduce the same empty match behavior (which was previously not allowed).Handling Empty Segments
To match both routes with and without additional path segments, you can register two routes:
This explicit approach ensures that you only handle empty segments when you intend to.
Security Consideration
A common scenario is serving static content using a route like
/static/*{filepath}
. In many routers (e.g., standardhttp.ServeMux
, Gin, Echo), this route matches empty segments by default, potentially allowing directory listing, of the base folder which can be a security concern.By not matching empty segments with catch-all wildcards, we can at least prevents unintended directory listing.
Future Considerations
Serving static files and handling directory listings are common needs. In the future, we plan to introduce an API helper to simplify serving static content.
Infix Wildcards
Catch-all wildcards can now be placed anywhere in the path, not just at the end.
Example:
They can also be followed or preceded by named parameters. For example,
/foo/*{bar}/{baz}
is valid and allows for flexible route definitions.Note: Catch-all wildcards cannot be consecutive. For example,
/foo/*{any1}/*{any2}
is not allowed. Since catch-all wildcards match at least one segment, the above pattern is equivalent to/foo/{param}/*{catchAll}
, which is allowed and more explicit about which segment matches each parameter.Differential Benchmark